//当前用户
import store from '../redux/store'
const current_user = () => store.getState().user.user || {};

// console.log('-------')
// UserLogin('abc','123').then((data)=>{
//   global.App.user_id = data.user_id
//   global.App.school_id = data.school_id
//   global.App.token = data.token
// AsyncStorage.setItem('NO','01');
// getItem(aKey,aCallback)
// })//  multiGet(aArrays,aCallBack)


const get_headers = ()=>{
  let base = {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    'user_id': loginState.user_id||'',
    'school_id': loginState.school_id||'',
    'token': loginState.token||'',
  }
  return base
}

//默认的GET Header
const _GET_ = () => {
  return {
    method: 'GET',
    headers: get_headers()
  }
};

//默认的POST Header
const _POST_ = (body) => {
  return (
    {
      method: 'POST',
      headers: get_headers(),
      body: JSON.stringify(body)
    }
  )
};

//默认的PUT Header
const _PUT_ = (body) => {
  return (
    {
      method: 'PUT',
      headers: get_headers(),
      body: JSON.stringify(body)
    }
  )
};

//默认的PUT Header
const _DELETE_ = (body) => {
  return (
    {
      method: 'DELETE',
      headers: get_headers(),
      body: JSON.stringify(body)
    }
  )
};


//通用的请求生成器
class Requester {
  constructor(header = {}, url) {
    this.host = App.host;
    this.version = "v1";
    this.url = url;
    this.header = header
  }

  do_fetch(url_improvement = true) {
    return new Promise((resolve, reject) => {
      let timeout = setTimeout(() => {
        reject("请求超时")
      }, 20000);
      let _url = `${this.host}/${this.url}`;
      fetch(_url, this.header).then((response) => {
        clearTimeout(timeout);
        if (response.status.toString().match(/^2.*/)) {
          return response.json()
        } else {
          return reject(response.status)
        }
      }).then((responseData) => {
        if(responseData.needLogin){
          App.logout(responseData.msg)
        }else{
          return resolve(responseData)
        }
      }).catch((error) => {
        return reject(error)
      })
    })
  }
}

export const UserLogin = (username, password, school_id=null) => {
  return new Requester(_POST_({username, password, school_id}), `app_api/users/login`).do_fetch()
};
export const GetAppliesApi = (page=1, business_type='', status='') => {
  return new Requester(_GET_(), `app_api/apply/list?page=${page}&business_type=${business_type}&status=${status}`).do_fetch()
};
export const GetApplyInfoApi = (id) => {
  return new Requester(_GET_(), `app_api/apply/show?id=${id}`).do_fetch()
};
export const UpdateApplyApi = (body) => {
  return new Requester(_PUT_(body), `app_api/apply/update`).do_fetch()
};
export const GetAuditsApi = (page=1, reply='') => {
  return new Requester(_GET_(), `app_api/apply/audits?page=${page}&reply=${reply}`).do_fetch()
};

export const GetExecutionsApi = (page=1, status='') => {
  return new Requester(_GET_(), `app_api/apply/executions?page=${page}&status=${status}`).do_fetch()
};

export const GetNoticesApi = (page=1) => {
  return new Requester(_GET_(), `app_api/notice/list?page=${page}`).do_fetch()
};

export const GetBroadcastsApi = (page=1) => {
  return new Requester(_GET_(), `app_api/broadcast/list?page=${page}`).do_fetch()
};

export const GetBroadcastApi = (broadcast_id) => {
  return new Requester(_GET_(), `app_api/broadcast/show?broadcast_id=${broadcast_id}`).do_fetch()
};

export const GetPlansApi = (day, onlyChangeMarkedDates=false) => {
  return new Requester(_GET_(), `app_api/plan/list?day=${day}&only_change_marked_dates=${onlyChangeMarkedDates}`).do_fetch()
};

export const GetPlanApi = (plan_id) => {
  return new Requester(_GET_(), `app_api/plan/show?plan_id=${plan_id}`).do_fetch()
};

export const CreatePlanApi = (body) => {
  return new Requester(_POST_(body), `app_api/plan/create`).do_fetch()
};

export const DestroyPlanApi = (body) => {
  return new Requester(_DELETE_(body), `app_api/plan/destroy`).do_fetch()
};

export const EditPlanApi = (plan_id) => {
  return new Requester(_GET_(), `app_api/plan/edit?plan_id=${plan_id}`).do_fetch()
};

export const UpdatePlanApi = (body) => {
  return new Requester(_PUT_(body), `app_api/plan/update`).do_fetch()
};

export const GetUsersApi = () => {
  return new Requester(_GET_(), `app_api/users/all`).do_fetch()
};